<?php
defined('_XR_EXEC') or die();
/**
 * 包装序列号的解开，压缩 
 */
class packet_Model extends Model
{

/* encode数组至少需要这些项

         	  [vege_id] =>  
            [vege_num] =>  
            [fixed] =>  
 */
	public function encode($rs,$rettype='string'){
		$re = array(); 
	  foreach($rs as $v){
			if($v['fixed'] == '1'){
				$re[] = $v['vege_id'].'-'.$v['vege_num'];
			}
		}
	  foreach($rs as $v){
			if($v['fixed'] == '0' && $v['vege_num'] > 0){
				$re[] = $v['vege_id'].'-'.$v['vege_num'];
			}
		}
		switch($rettype){
			case "string":
				$re = implode(',',$re);
				break;
			default://array
				
				break;	
		}
		return $re;
	}
	public function decode($rs,$rettype='array'){
		$db = factory::getDBO();
		$re = array();
		$packet = explode(',',$rs);
		$re_str = array();
		$re_html = array();
		foreach($packet as $val){
			$vegeobj = explode('-',$val);
			$rs_vegeobj = $db->loadassoc('select id,name,code from t_vege where id="'.$vegeobj[0].'" ');
			$re[] = array(
				"id"=>$rs_vegeobj['id'],
				"code"=>$rs_vegeobj['code'],
				"name"=>$rs_vegeobj['name'],
				"num"=>$vegeobj[1]
			);
			$re_str[] = $rs_vegeobj['name'].'('.$rs_vegeobj['code'].') '.$vegeobj[1].' 份';
			$re_html[] = '<div class="vegediv">'.$rs_vegeobj['name'].'('.$rs_vegeobj['code'].') <span class="vege_num">'.$vegeobj[1].'</span> 份</div>';
		}
		switch($rettype){
			case "string":
				$re = implode(',',$re_str);
				break;
			case "html":
				$re = implode(' ',$re_html);
				break;
			default://array
				
				break;	
		}
		return $re;
	}
	
	
	
	 
	 
}

?>